layui.config({
    base : "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index','form','jquery', 'table'],function(){
    var form = layui.form,
        table=layui.table,
        admin  = layui.admin;

    /**
     * 防重复设置
     * @type {boolean}
     */
     var  edit_flag = true;


    form.verify({
        FinalRate: function (value) {

            if (value<0||value>0.6) {
                return '微信结算费率设置不合理';
            }
        },
        SubsidyRate: function (value) {
            if (value<0||value>0.2) {
                //小于0.2
                return '补贴设置不合理';
            }
        }
    });



    table.render({
        elem: '#oem_sms_info_table'
        ,url: '/sms/queryData'
        ,method:"post"
        ,height: 500
        ,limit:50							//显示数目
        ,limits:[50,100]
        ,cols: [[
            {field:'sysName',width:150, title: '短信模板名',"align":"center"}
            ,{field:'sysParams',width:150, title: '短信模板参数',"align":"center"}
            ,{field:'template_code',width:150, title: '短信模板code',"align":"center"}
            ,{field:'createdTime',width:200, title: '创建时间',"align":"center"}
            ,{field:'OPT', width:200, title: '操作',"align":"center",toolbar:"#OptData"}
        ]]
        ,request:{
            pageName: 'pageNo' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }

        ,id: 'oem_sms_info_table'
        ,page: true
    });

    table.on('tool(oem_sms_info_table)', function(obj){

        var data = obj.data;

        $("#edit_sms_form input[name='sysName']").attr("disabled","true");
        $("#edit_sms_form input[name='sysParams']").attr("disabled","true");




            edit_flag = true;
            admin.popup({
                title: '编辑'
                ,shade: 0
                ,anim: -1
                ,offset:"20px"
                ,area: ['80%', '500px']
                ,skin: 'layui-anim layui-anim-upbit'
                ,content:$("#add_agencyUser_win")
                ,end:function () {
                    table.reload('oem_sms_info_table', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                        }
                    });
                }
            });
            form.val('edit_sms_form', {
                "smsId":data.smsId,
                "sysId":data.sysId,
                "sys_config_id":data.sys_config_id,
                "sysParams":data.sysParams,
                "sysName":data.sysName,
                "template_code":data.template_code
            });

    });



    /**
     * 短信配置编辑提交
     */
    form.on('submit(editsmsinfo)',function (data) {
        if(edit_flag){
            edit_flag= false;
            $.ajax({
                url:"/sms/edit",
                data:data.field,
                type:"post",
                success:function (result) {
                    if (result.code == 1){
                        edit_flag=true;
                        layer.msg("编辑成功",{
                            offset: '32%'
                            ,icon: 1
                            ,time: 0
                            ,btn: '知道了'
                            ,btnAlign:"c"
                            ,shade:0.4
                            ,yes:function () {
                                table.reload('oem_sms_info_table', {
                                    where: {
                                        userName:$(".queryTable input[name='userName']").val()
                                    }
                                });
                                layer.closeAll();
                            }
                        })
                    }else{
                        edit_flag=true;
                        layer.msg(result.message,{
                            offset: '32%'
                            ,icon: 2
                            ,time: 3000
                        },function () {
                            layer.closeAll();
                        })
                    }

                }
            });
        }else {
            layer.msg("不能重复提交表单",{
                offset: '32%'
                ,icon: 2
                ,time: 3000
            },function () {

            })
        }

        return false;
    });
    

    
});



